#pragma once
#include <iostream>
#include <vector>

namespace ys
{
	template<class T>
	class less
	{
	public:
		bool operator()(const T& first, const T& second)
		{
			return first < second;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& first, const T& second)
		{
			return first > second;
		}
	};

	template<class T, class Container = std::vector<T>,
		class Compare = less<typename Container::value_type>>
	class priority_queue
	{
	private:
		void adjust_up(size_t child)
		{
			Compare com;
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[child], _con[parent]))
				{
					std::swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
					break;
			}
		}
		void adjust_down(size_t parent)
		{
			Compare com;
			size_t child = (parent * 2) + 1;
			while (child < size())
			{
				if (child + 1 < _con.size() && com(_con[child + 1], _con[child]))
					std::swap(_con[child], _con[child + 1]);
				if (com(_con[child], _con[parent]))
				{
					std::swap(_con[child], _con[parent]);
					parent = child;
					child = (parent * 2) + 1;
				}
				else
					break;
			}
		}
	public:
		typedef T value_type;
		//--------------------构造-------------------
		priority_queue(){}

		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_con.push_back(*first);
				first++;
			}

			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
			{
				adjust_down(i);
			}
		}
		void push(const value_type& val)
		{
			_con.push_back(val);
			adjust_up(_con.size() - 1);
		}
		void pop()
		{
			std::swap(_con[0], _con[size() - 1]);
			_con.pop_back();

			adjust_down(0);
		}
		size_t size() const
		{
			return _con.size();
		}
		bool empty() const
		{
			return _con.empty();
		}
		value_type& top()
		{
			return _con[0];
		}
		void swap(priority_queue& pq)
		{
			_con.swap(pq._con);
		}
	private:
		Container _con;
	};
}